package com.ruoyi.business.service.koc.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.business.domain.koc.TbKocTask;
import com.ruoyi.business.domain.koc.TbKocTaskType;
import com.ruoyi.business.domain.vo.TbKocTaskAllVO;
import com.ruoyi.business.mapper.koc.TbKocTaskMapper;
import com.ruoyi.business.mapper.koc.TbKocTaskTypeMapper;
import com.ruoyi.business.service.koc.ITbKocTaskService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * koc 任务服务层实现
 *
 * @author chenran
 */
@Service
public class TbKocTaskServiceImpl extends ServiceImpl<TbKocTaskMapper, TbKocTask> implements ITbKocTaskService {

    @Resource
    private TbKocTaskTypeMapper tbKocTaskTypeMapper;

    @Override
    public TableDataInfo<TbKocTask> selectPageList(String name, PageQuery pageQuery) {
        LambdaQueryWrapper<TbKocTask> eq = Wrappers.lambdaQuery(TbKocTask.class);
        if (StringUtils.isNotEmpty(name)) {
            eq.like(TbKocTask::getName, name);
        }
        eq.orderByDesc(TbKocTask::getCreateTime);
        Page<TbKocTask> page = this.page(pageQuery.build(), eq);
        return TableDataInfo.build(page);
    }

    @Override
    public TbKocTask queryById(Long id) {
        return this.baseMapper.selectById(id);
    }

    @Override
    public List<TbKocTask> queryByTaskId(Long taskTypeId) {
        LambdaQueryWrapper<TbKocTask> eq = Wrappers.lambdaQuery(TbKocTask.class)
            .eq(TbKocTask::getTaskTypeId, taskTypeId);
        return this.baseMapper.selectList(eq);
    }

    @Override
    public List<TbKocTaskAllVO> queryAll() {
        List<TbKocTask> tbKocTasks = this.baseMapper.selectList();
        Set<Long> taskTypeId = tbKocTasks.stream().map(TbKocTask::getTaskTypeId).collect(Collectors.toSet());
        List<TbKocTaskType> tbKocTaskTypeList = tbKocTaskTypeMapper.selectList(Wrappers.lambdaQuery(TbKocTaskType.class).in(TbKocTaskType::getId, taskTypeId));
        Map<Long, TbKocTaskType> taskTypeMap = tbKocTaskTypeList.stream().collect(Collectors.toMap(TbKocTaskType::getId, Function.identity()));
        List<TbKocTaskAllVO> resVO = new ArrayList<>();
        tbKocTasks.forEach(task -> {
            TbKocTaskType tbKocTaskType = taskTypeMap.get(task.getTaskTypeId());
            TbKocTaskAllVO tbKocTaskAllVO = BeanUtil.toBean(task, TbKocTaskAllVO.class);
            tbKocTaskAllVO.setTaskTypeName(tbKocTaskType.getName());
            resVO.add(tbKocTaskAllVO);

        });
        return resVO;
    }
}
